package com.wlz.controller;

import com.wlz.dao.UserRepository;
import com.wlz.model.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author： wlz
 * @Date 2020/12/23 17:18
 * @description： TODO
 * @version: 1.0
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserRepository userRepository;

    @GetMapping("")
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    @GetMapping("/{userId}")
    public User getByUserId(@PathVariable String userId) {
        return userRepository.findById(userId).orElse(new User());
    }

    @PostMapping("")
    public User addNewUser(@RequestBody User user) {
        return userRepository.save(user);
    }

    @DeleteMapping("/{userId}")
    public String delete(@PathVariable String userId) {
        User user = new User();
        user.setUserId(userId);
        userRepository.deleteById(userId);
        return "deleted: " + userId;
    }

    @PutMapping("")
    public User update(@RequestBody User user) {
        return userRepository.save(user);
    }


    @GetMapping("/findByAge")
    public List<User> findByAge(@RequestParam("age")Integer age) {
        return userRepository.findByAge(age);
    }

//    @GetMapping("/findByAge")
//    public List<User> findByAge(@PathVariable Integer age) {
//        return userRepository.findByAge(age,new Pageable());
//    }


}
